﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class BFileUploadInstruction:InstructionBase
    {
        #region props
        private string _SrcFolderPath;
        /// <summary></summary>
        public string SrcFolderPath
        {
            get
            {
                return this._SrcFolderPath;
            }
            set
            {
                this._SrcFolderPath = value;
            }
        }

        private bool _IncludeSubFolder;
        /// <summary></summary>
        public bool IncludeSubFolder
        {
            get
            {
                return this._IncludeSubFolder;
            }
            set
            {
                this._IncludeSubFolder = value;
            }
        }

        private string _Filter;
        /// <summary></summary>
        public string Filter
        {
            get
            {
                return this._Filter;
            }
            set
            {
                this._Filter = value;
            }
        }

        private string _ConnStr;
        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._ConnStr;
            }
            set
            {
                this._ConnStr = value;
            }
        }

        private string _TableName;
        /// <summary></summary>
        public string TableName
        {
            get
            {
                return this._TableName;
            }
            set
            {
                this._TableName = value;
            }
        }

        private string _FileNameField;
        /// <summary></summary>
        public string FileNameField
        {
            get
            {
                return this._FileNameField;
            }
            set
            {
                this._FileNameField = value;
            }
        }

        private string _FilePathField;
        /// <summary></summary>
        public string FilePathField
        {
            get
            {
                return this._FilePathField;
            }
            set
            {
                this._FilePathField = value;
            }
        }

        private string _ImageFieldName;
        /// <summary></summary>
        public string ImageFieldName
        {
            get
            {
                return this._ImageFieldName;
            }
            set
            {
                this._ImageFieldName = value;
            }
        }

        private string _KeyFieldName;
        /// <summary></summary>
        public string KeyFieldName
        {
            get
            {
                return this._KeyFieldName;
            }
            set
            {
                this._KeyFieldName = value;
            }
        }
        #endregion

        /// <summary></summary>
        public BFileUploadInstruction() : 
            base(Guid.Empty, Guid.Empty)
        {
            this._SrcFolderPath = string.Empty;
            this._Filter = "*.*";
            this._IncludeSubFolder = true;
            this._ConnStr = string.Empty;
            this._TableName = string.Empty;
            this._FileNameField = string.Empty;
            this._FilePathField = string.Empty;
            this._ImageFieldName = string.Empty;
            this._KeyFieldName = string.Empty;
        }

        /// <summary></summary>
        public BFileUploadInstruction(Guid componentID, Guid jobTicketID,
            string srcFolderPath, 
            string filter,
            bool includeSubFolders,
            string connStr,
            string tblName,
            string fileNameField,
            string filePathFiled,
            string imageField,
            string keyFieldName)
            :base(componentID,jobTicketID)
        {
            this._SrcFolderPath = srcFolderPath;
            this._Filter = filter;
            this._IncludeSubFolder = includeSubFolders;
            this._ConnStr = connStr;
            this._TableName = tblName;
            this._FileNameField = fileNameField;
            this._FilePathField = filePathFiled;
            this._ImageFieldName = imageField;
            this._KeyFieldName = keyFieldName;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcFolderPath", this.SrcFolderPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Filter", this.Filter);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IncludeSubFolder", this.IncludeSubFolder.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableName", this.TableName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileNameField", this.FileNameField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FilePathField",this.FilePathField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ImageField", this.ImageFieldName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "KeyField", this.KeyFieldName);

            return xNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                BFileUploadInstruction instruction = new BFileUploadInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.SrcFolderPath = xNode.Attributes.GetNamedItem("SrcFolderPath").Value;
                instruction.Filter = xNode.Attributes.GetNamedItem("Filter").Value;
                instruction.IncludeSubFolder = bool.Parse(xNode.Attributes.GetNamedItem("IncludeSubFolder").Value);
                instruction.ConnStr = xNode.Attributes.GetNamedItem("ConnStr").Value;
                instruction.TableName = xNode.Attributes.GetNamedItem("TableName").Value;
                instruction.FileNameField = xNode.Attributes.GetNamedItem("FileNameField").Value;
                instruction.FilePathField = xNode.Attributes.GetNamedItem("FilePathField").Value;
                instruction.ImageFieldName = xNode.Attributes.GetNamedItem("ImageField").Value;
                instruction.KeyFieldName = xNode.Attributes.GetNamedItem("KeyField").Value;

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }
    }
}
